import requests
from lxml import etree
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["4399"]
collection = db["动作游戏"]

l = []

for i in range(1,6):
    responses = requests.get(f'https://www.4399.com/flash_fl/more_2_{i}.htm')
    tree = etree.HTML(responses.content.decode('gbk'))
    # 解析1
    # href = tree.xpath('//li/a/@href')
    # img= tree.xpath('//li/a/img/@lz_src')
    # name = tree.xpath('//li/a/img/@alt')

    # 解析二
    datas = tree.xpath('//ul[@class ="list affix cf"]/li/a/img')
    for data in datas:
        d = {
            'href': data.xpath('../@href')[0],
            "img":data.attrib['lz_src'],
            "title":data.attrib['alt'],
        }
        l.append(d)
collection.insert_many(l)







